package com.huawei.admins.platform.gateway.core.component.limiter;

import com.huawei.admins.platform.gateway.core.component.filter.factory.RequestRateLimiterGatewayFilterFactory.CompositeConfig;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.Map;

public interface RateLimiter {

    Mono<Response> isAllowed(String routeId, String id, CompositeConfig config);

    class Response {

        private final boolean allowed;

        private final long tokensRemaining;

        private final Map<String, String> headers;

        public Response(boolean allowed, Map<String, String> headers) {
            this.allowed = allowed;
            this.tokensRemaining = -1;
            Assert.notNull(headers, "headers may not be null");
            this.headers = headers;
        }

        public boolean isAllowed() {
            return allowed;
        }

        public Map<String, String> getHeaders() {
            return Collections.unmodifiableMap(headers);
        }

        @Override
        public String toString() {
            final StringBuffer sb = new StringBuffer("Response{");
            sb.append("allowed=").append(allowed);
            sb.append(", headers=").append(headers);
            sb.append(", tokensRemaining=").append(tokensRemaining);
            sb.append('}');
            return sb.toString();
        }

    }

}
